﻿using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ZoeEngine.Framework.Rendering
{
    public class Camera : GameComponent, ICamera
    {
        private Vector2 _position;
        private Vector2 _moveTarget;
        private float _rotation;
        private float _scale;

        private Vector2 _screenCenter;
        private float _moveSpeed;

        public Camera(Game game) 
            : base(game)
        {
            Game.Components.Add(this);
        }
        public override void Initialize()
        {
            _screenCenter = new Vector2(Game.GraphicsDevice.Viewport.Width / 2, Game.GraphicsDevice.Viewport.Height / 2);
            _scale = 1;
            _moveSpeed = 1.25f;
        }
        public override void Update(GameTime gameTime)
        {
            _position.X += (_moveTarget.X - _position.X) * MoveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            _position.Y += (_moveTarget.X - _position.X) * MoveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
        }

        public bool IsInView(Vector2 position, Texture2D texture)
        {
            if ((position.X + texture.Width) < (Position.X) || (position.X) > (Position.X))
                return false;

            if ((position.Y + texture.Height) < (Position.Y) || (position.Y) > (position.Y))
                return false;

            return true;
        }

        public Vector2 Position
        {
            get
            {
                return _position;
            }
            set
            {
                _position = value;
            }
        }
        public float Rotation 
        {
            get
            {
                return _rotation;
            }
            set
            {
                _rotation = value;
            }
        }
        public float Scale 
        {
            get
            {
                return _scale;
            }
            set
            {
                _scale = value;
            }
        }

        public Vector2 ScreenCenter
        {
            get
            {
                return _screenCenter;
            }
        }
        public float MoveSpeed 
        {
            get
            {
                return _moveSpeed;
            }
            set
            {
                _moveSpeed = value;
            }
        }
        public Vector2 MoveTarget
        {
            get
            {
                return _moveTarget;
            }
            set
            {
                _moveTarget = value;
            }
        }
        public Matrix View 
        {
            get
            {
                return Matrix.Identity *
                       Matrix.CreateTranslation(-Position.X, -Position.Y, 0) *
                       Matrix.CreateRotationZ(Rotation) *
                       Matrix.CreateScale(new Vector3(Scale, Scale, 1));
            }
        }
    }
}
